package dao

import (
	"miao/config"
	"miao/models"
)

// GetUserbyUsername UserBaseDao 用户表数据访问对象
func GetUserbyUsername(username string) (models.UserBase, error) {
	user := models.UserBase{}
	config.DB.Where("username = ?", username).Take(&user)
	return user, nil
}

// Page @description: 分页查询用户
// @param currentPage 当前页码
// @param pageSize 每页大小
func Page(currentPage int, pageSize int, username string) ([]models.UserBase, int64, error) {
	var users []models.UserBase
	var total int64
	config.DB.Model(&models.UserBase{}).Where("username LIKE ?", "%"+username+"%").Count(&total)
	offset := (currentPage - 1) * pageSize
	config.DB.Where("username LIKE ?", "%"+username+"%").Limit(pageSize).Offset(offset).Find(&users)
	return users, total, nil
}

// DeleteUser @description: 删除用户
// @param userId 用户ID
func DeleteUser(userId string) error {
	config.DB.Where("id = ?", userId).Delete(&models.UserBase{})
	return nil
}

func SaveUser(user *models.UserBase) error {
	config.DB.Create(user)
	return nil
}
